package javolution.xml.stream;

import java.util.Collection;
import java.util.Map;
import javolution.lang.Reusable;
import javolution.text.CharArray;
import javolution.util.FastCollection;
import kotlin.text.Typography;

/* loaded from: classes2.dex */
final class EntitiesImpl implements Reusable {
    private Map _entitiesMapping;
    private int _maxLength = 1;
    private CharArray _tmp = new CharArray();

    public Map getEntitiesMapping() {
        return this._entitiesMapping;
    }

    public int getMaxLength() {
        return this._maxLength;
    }

    public int replaceEntity(char[] cArr, int i, int i2) throws XMLStreamException {
        int i3 = i + 1;
        int i4 = 0;
        if (cArr[i3] == '#') {
            char c = cArr[i + 2];
            int i5 = c == 'x' ? 16 : 10;
            for (int i6 = c == 'x' ? 3 : 2; i6 < i2 - 1; i6++) {
                char c2 = cArr[i + i6];
                i4 = (i4 * i5) + (c2 <= '9' ? c2 - '0' : c2 <= 'Z' ? c2 - 'A' : c2 - 'a');
            }
            cArr[i] = (char) i4;
            return 1;
        } else if (cArr[i3] == 'l' && cArr[i + 2] == 't' && cArr[i + 3] == ';') {
            cArr[i] = Typography.less;
            return 1;
        } else if (cArr[i3] == 'g' && cArr[i + 2] == 't' && cArr[i + 3] == ';') {
            cArr[i] = Typography.greater;
            return 1;
        } else if (cArr[i3] == 'a' && cArr[i + 2] == 'p' && cArr[i + 3] == 'o' && cArr[i + 4] == 's' && cArr[i + 5] == ';') {
            cArr[i] = '\'';
            return 1;
        } else if (cArr[i3] == 'q' && cArr[i + 2] == 'u' && cArr[i + 3] == 'o' && cArr[i + 4] == 't' && cArr[i + 5] == ';') {
            cArr[i] = Typography.quote;
            return 1;
        } else if (cArr[i3] == 'a' && cArr[i + 2] == 'm' && cArr[i + 3] == 'p' && cArr[i + 4] == ';') {
            cArr[i] = Typography.amp;
            return 1;
        } else {
            this._tmp.setArray(cArr, i3, i2 - 2);
            Map map = this._entitiesMapping;
            CharSequence charSequence = map != null ? (CharSequence) map.get(this._tmp) : null;
            if (charSequence != null) {
                int length = charSequence.length();
                while (i4 < length) {
                    cArr[i + i4] = charSequence.charAt(i4);
                    i4++;
                }
                return length;
            }
            throw new XMLStreamException("Entity " + ((Object) this._tmp) + " not recognized");
        }
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._maxLength = 1;
        this._entitiesMapping = null;
    }

    public void setEntitiesMapping(Map map) {
        Collection<CharSequence> values = map.values();
        if (values instanceof FastCollection) {
            FastCollection fastCollection = (FastCollection) values;
            FastCollection.Record head = fastCollection.head();
            FastCollection.Record tail = fastCollection.tail();
            while (true) {
                head = head.getNext();
                if (head == tail) {
                    break;
                }
                CharSequence charSequence = (CharSequence) fastCollection.valueOf(head);
                if (this._maxLength < charSequence.length()) {
                    this._maxLength = charSequence.length();
                }
            }
        } else {
            for (CharSequence charSequence2 : values) {
                if (this._maxLength < charSequence2.length()) {
                    this._maxLength = charSequence2.length();
                }
            }
        }
        this._entitiesMapping = map;
    }
}
